$import("com.joypard.platform.account.user.UserDI");

/**
 * 人员视图
 */
K.defView("com.joypard.platform.organization.org.UserView", {
	organizatoinView : null, // 构造参数

	/**
	 * 权限配置
	 */
	purviewConfig : {
		view : "platform.organization.org.user",
		addUser : "platform.organization.org.user.addUser",
		delUser : "platform.organization.org.user.delUser"
	},

	/**
	 * 构造方法
	 * 
	 * @param organizatoinView
	 *            组织视图
	 */
	UserView : function(organizatoinView) {
		this.organizatoinView = organizatoinView;
		E.addListener("组织机构树-单击", this.onClickOrgTreeNode, this);
		E.addEvents("组织-人员-添加");
		E.addEvents("组织-人员-移除");
		E.addListener("组织-信息改变", this.onOrgChangeEvent, this);
		E.addListener("组织-人员-添加-成功", this.onAddOrgUsersSuccessEvent, this);
		E.addListener("组织-人员-移除-成功", this.onDelOrgUsersSuccessEvent, this);
	},

	/**
	 * 人员面板
	 */
	createUserPanel : function() {
		this.curOrgLabel = new Ext.form.Label();
		var me = this, options = {
			title : "人员信息",
			iconCls : "p-tab",
			tbar : [ {
				xtype : "label",
				text : "当前组织："
			}, this.curOrgLabel, {
				text : "",
				hideMode : "visibility",
				hidden : true
			} ],
			layout : "vbox",
			defaults : {
				width : "100%",
				headerCssClass : "x-panel-header-no-border-t-l-r",
				bodyCssClass : "x-panel-body-no-border-t-l"
			},
		};

		this.orgUserStore = UserDI.createUsersByOrgCodeStore(); // 按组织ID查找人员的数据源
		this.orgUserGrid = this.createOrgUserGrid(this.orgUserStore); // 组织用户列表

		this.userGridPanel = this.createUserGridPanel(); // 所有用户列表
		this.userInfoEditGrid = this.createUserInfoEditGrid(); // 创建下面的"添加组织人员面板"

		this.selectedUserGridPanel = this.createSelectedUserGridPanel(this.orgUserStore); // 已加入组织的人员列表
		// 拖拽功能
		this.selectedUserGridPanel.on("afterrender", function() {
			if (UserView.purviewAddUser()) { // 用户列表 -> 已加入组织用户列表
				var selectedUserGridPanelDropTargetEl = me.selectedUserGridPanel.getView().scroller.dom;
				new Ext.dd.DropTarget(selectedUserGridPanelDropTargetEl, {
					ddGroup : 'selectedUserGridDDGroup',
					notifyDrop : function(ddSource, e, data) {
						var records = ddSource.dragData.selections;
						E.fireEvent("组织-人员-添加", me.curOrgNode, records);
					}
				});
			}

			if (UserView.purviewDelUser()) { // 已加入组织用户列表 -> 用户列表
				var userGridPanelDropTargetEl = me.userGridPanel.getView().scroller.dom;
				new Ext.dd.DropTarget(userGridPanelDropTargetEl, {
					ddGroup : 'userGridDDGroup',
					notifyDrop : function(ddSource, e, data) {
						var records = ddSource.dragData.selections;
						E.fireEvent("组织-人员-移除", me.curOrgNode, records);
					}
				});
			}
		});

		// 当人员列表被渲染完成后加载数据
		this.userGridPanel.on("afterrender", function() {
			this.getStore().load();
		});

		this.userInfoEditGrid.add(this.userGridPanel); // 添加左边的面板
		this.userInfoEditGrid.add(this.selectedUserGridPanel); // 添加右边的面板

		this.userPanel = new K.Ext.Panel(options); // 标签页面板

		this.userPanel.add(this.orgUserGrid); // 添加上面的面板
		this.userPanel.add(this.userInfoEditGrid); // 添加下面的面板

		return this.userPanel;
	},

	/**
	 * 人员列表面板
	 */
	createOrgUserGrid : function(orgUserStore) {
		var options = {
			title : "组织人员",
			flex : .25,
			iconCls : "p-grid",
			store : orgUserStore,
			columns : [ {
				header : "姓名",
				dataIndex : "userName"
			}, {
				header : "手机",
				dataIndex : "mobile"
			} ]
		};
		return new K.Ext.grid.GridPanel(options);
	},

	/**
	 * 添加人员信息
	 */
	createUserInfoEditGrid : function() {
		var options = {
			title : "添加组织人员",
			bodyStyle : "border-bottom:0px;",
			flex : .55,
			iconCls : "p-edit",
			defaults : {
				viewConfig : {
					forceFit : true
				}
			},
			layout : "hbox",
			layoutConfig : {
				align : "stretch" // +flex
			}
		};
		return new K.Ext.Panel(options);
	},

	/**
	 * 组织人员面板
	 */
	createUserGridPanel : function() {
		var me = this, store = UserDI.createGetUsersStore();
		this.userGridUserNameTextField = new Ext.form.TextField({
			xtype : "textfield",
			emptyText : "姓名",
			width : "60"
		});
		this.userGridMobileTextField = new Ext.form.TextField({
			xtype : "textfield",
			emptyText : "手机",
			width : "100"
		});
		this.userGridSearchButton = new Ext.Button({
			iconCls : "search",
			text : "查询",
			handler : K.createDelegate(this.onOrgUsersSearch, this)
		});
		function setKeyHandler(text) {
			new Ext.KeyMap(text.getEl(), [ {
				key : [ 10, 13 ],
				fn : function() {
					me.onOrgUsersSearch();
				},
				scope : this
			} ]);
		}
		// 绑定回车键
		this.userGridUserNameTextField.on("afterrender", setKeyHandler, this);
		this.userGridMobileTextField.on("afterrender", setKeyHandler, this);
		var options = {
			flex : 0.6,
			headerStyle : "border-top:0px;border-left:0px;",
			bodyStyle : "border-left:0px;",
			margins : "0 1 0 0",
			ddGroup : "selectedUserGridDDGroup",
			enableDragDrop : true,
			tbar : {
				xtype : "toolbar",
				style : "border-top:0px;border-left:0px;",
				items : [ this.userGridUserNameTextField, ' ', this.userGridMobileTextField, '->', this.userGridSearchButton ]
			},
			columns : [ {
				header : "姓名",
				dataIndex : "userName"
			}, {
				header : "手机",
				dataIndex : "mobile"
			} ],
			store : store,
			bbar : {
				xtype : "pagination",
				style : "border-bottom:0px;border-left:0px;",
				displayMsg : ""
			}

		};
		return new K.Ext.grid.GridPanel(options);
	},

	/**
	 * 创建已选择的组织人员
	 */
	createSelectedUserGridPanel : function(orgUserStore) {
		this.curOrgTextField = new Ext.form.TextField({
			emptyText : "请单击组织树节点",
			readOnly : true
		});
		var options = {
			flex : 0.4,
			ddGroup : "userGridDDGroup",
			enableDragDrop : true,
			store : orgUserStore,
			headerStyle : "border-top:0px;border-right:0px;",
			bodyStyle : "border-right:0px;border-bottom:0px;",
			tbar : {
				xtype : "toolbar",
				style : "border-top:0px;border-right:0px;",
				items : [ {
					xtype : "label",
					text : "当前组织："
				}, this.curOrgTextField, {
					text : "",
					hideMode : "visibility",
					hidden : true
				} ]
			},
			columns : [ {
				header : "姓名",
				dataIndex : "userName"
			}, {
				header : "手机",
				dataIndex : "mobile"
			} ]
		};
		return new K.Ext.grid.GridPanel(options);
	},

	/**
	 * 组织树事件监听器,加载当前组织的岗位信息
	 */
	onClickOrgTreeNode : function(orgNode) {
		this.curOrgNode = orgNode;
		this._updateCurOrgInfo(orgNode);
		this.orgUserGrid.getStore().load({
			params : {
				orgId : orgNode.id
			}
		});
	},

	/**
	 * 添加组织用户成功事件处理
	 */
	onAddOrgUsersSuccessEvent : function(output) {
		var records = output.getParameter("userRecords");
		K.Ext.flyStore(this.selectedUserGridPanel.getStore()).add(records);
		K.m("添加成功").show(this.selectedUserGridPanel.tbar);
	},

	/**
	 * 删除组织用户成功事件处理
	 */
	onDelOrgUsersSuccessEvent : function(output) {
		var records = output.getParameter("userRecords"), store = this.selectedUserGridPanel.getStore();
		Ext.each(records, store.remove, store);
		K.m("移除成功").show(this.selectedUserGridPanel.tbar);
	},

	/**
	 * 点击查询
	 */
	onOrgUsersSearch : function() {
		var params = {};
		params.userNickName = this.userGridUserNameTextField.getValue();
		params.mobile = this.userGridMobileTextField.getValue();
		this.userGridPanel.getStore().load({
			params : params
		});
	},
	// 结束事件监听器

	/**
	 * 创建用户下拉选择框
	 */
	createUserSelectCombo : function(config) {
		var orgNode = this.orgView.getOrgSelectTreeNode();
		config.storeBaseParams = {
			orgId : orgNode.id
		};
		K.extend(config);
		return UserView.createUserLovCombo(config);
	},

	/**
	 * 当机构信息被改变时
	 */
	onOrgChangeEvent : function(orgNode) {
		this._updateCurOrgInfo(orgNode);
	},
	/**
	 * 更新当前组织信息
	 */
	_updateCurOrgInfo : function(orgNode) {
		var orgName = orgNode.text;
		this.curOrgTextField.setValue(orgName);
		this.curOrgLabel.setText(orgName);
	},

	// 静态属性及方法定义
	statics : {
		/**
		 * 创建一个用户下拉列表
		 */
		createUserLovCombo : function(config) {
			var options = {
				triggerAction : 'all',
				valueField : 'id',
				displayField : 'userName',
				checkModel : 'single'
			};
			config.store = config.store || UserDI.createUsersByOrgCodeStore({
				baseParams : config.storeBaseParams,
				params : config.storeParams
			});
			K.extend(options, config);
			return new Ext.ux.form.LovCombo(options);
		}
	}
});